/*
 * Copyright (C) 2013 TinhVan Outsourcing.
 */
package com.getinstaliker.util.network.httptask;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;

import android.content.Context;
import android.util.Log;

import com.getinstaliker.util.network.handler.JsonPaserExceptions;
import com.getinstaliker.util.network.util.HttpUtil;
import com.getinstaliker.util.network.util.JsonUtil;

/**
 * The Class MobiNetworkJsonTask.
 *
 * @param <Param> the generic type
 * @param <Result> the generic type
 */
public abstract class MobiNetworkJsonTask<Param, Result> extends MobiNetworkTask<Param, Void, Result> {
    
    /** The m type. */
    private Class<Result> mType;

    /**
     * Instantiates a new mobi network json task.
     *
     * @param pContext the context
     * @param pShowLoading the show loading
     */
    public MobiNetworkJsonTask(Context pContext, boolean pShowLoading) {
        super(pContext, pShowLoading);
        this.mType = this.getType();
    }

    /**
     * Instantiates a new mobi network json task.
     *
     * @param pContext the context
     */
    public MobiNetworkJsonTask(Context pContext) {
        super(pContext, false);
        this.mType = this.getType();
    }

    // get parameter type from generic class
    // in class return type is result type
    /** The index result type. */
    final int indexResultType = 1;

//    /**
//     * Gets the type.
//     *
//     * @return the type
//     */
//    private Class<Result> getType() {
//        @SuppressWarnings("unchecked")
//        Class<Result> classType =
//                (Class<Result>) ((ParameterizedType) getClass().getGenericSuperclass())
//                        .getActualTypeArguments()[indexResultType];
//        return classType;
//    }
    /**
     * Gets the type.
     *
     * @return the type
     */
    protected abstract Class<Result> getType();

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.smartebook.android.mobibook.networkhandler.networktask.MobiNetworkTask#processInBackground
     * (Params[])
     */
    @Override
    public Result processInBackground(Param... pParams) throws IOException {
        final String json = HttpUtil.getStringResponseByGetMethod((String) pParams[0]);
        Log.i("MobiNetworkJsonTask", " Json :" + json);
        Result result = null;
        try {
            result = JsonUtil.fromString(json, mType);
            onDoAfterPaser(result);
        } catch (JsonPaserExceptions e) {
            e.printStackTrace();
            onJsonPaserException(e,mContext.get(),pParams);
        }
        return result;
    }

    /**
     * On json paser exception.
     *
     * @param exception the exception
     * @param pContext the context
     * @param param the param
     */
    protected void onJsonPaserException(Exception exception, Context pContext, Param... param) {

    }
    
    /**
     * On do after paser.
     *
     * @param result the result
     */
    protected void onDoAfterPaser(Result result) {
        
    }
}
